package cn.edu.scau.cmi.linhuanrui.client;

import cn.edu.scau.cmi.linhuanrui.adapter.Student;
import cn.edu.scau.cmi.linhuanrui.adapter.AdapterOfClass;
import cn.edu.scau.cmi.linhuanrui.adapter.AdapterOfObject;

import java.util.ArrayList;
import java.util.List;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.application.Application;
import javafx.scene.layout.AnchorPane;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;

public class AdapterClient extends Application {
	private List<Student> student1 = new ArrayList<>();
	private List<Student> student2 = new ArrayList<>();
	private AdapterOfClass adapterOfClass = new AdapterOfClass();
	private AdapterOfObject adapterOfObject = new AdapterOfObject();

	public void start(Stage primaryStage){
		primaryStage.setTitle("适配器模式");
		primaryStage.setResizable(false);

		Label label1 = new Label("类适配器\n\n请输入学生学号:\n\n请输入学生姓名:");
		label1.setLayoutX(20);
		label1.setLayoutY(20);
		TextField textField1 = new TextField();
		textField1.setLayoutX(120);
		textField1.setLayoutY(45);
		TextField textField2 = new TextField();
		textField2.setLayoutX(120);
		textField2.setLayoutY(75);
		Button button1 = new Button("添加该学生并输出名单");
		button1.setLayoutX(300);
		button1.setLayoutY(75);
		TextArea textArea1 = new TextArea();
		textArea1.setLayoutX(20);
		textArea1.setLayoutY(110);

		Label label2 = new Label("对象适配器\n\n请输入学生学号:\n\n请输入学生姓名:");
		label2.setLayoutX(20);
		label2.setLayoutY(300);
		TextField textField3 = new TextField();
		textField3.setLayoutX(120);
		textField3.setLayoutY(325);
		TextField textField4 = new TextField();
		textField4.setLayoutX(120);
		textField4.setLayoutY(355);
		Button button2 = new Button("添加该学生并输出名单");
		button2.setLayoutX(300);
		button2.setLayoutY(355);
		TextArea textArea2 = new TextArea();
		textArea2.setLayoutX(20);
		textArea2.setLayoutY(390);

		button1.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent event) {
				String id = textField1.getText();
				String name = textField2.getText();
				textArea1.clear();
				if (checkTextF(id, name, textArea1)) {
					Student student = new Student(Integer.parseInt(id), name);
					for (int i = 0; i < student1.size(); i++) {
						if (student1.get(i).getID() == student.getID()) {
							textArea1.appendText("该学号的学生已存在！");
							return;
						} 
					}
					student1.add(student);
					adapterOfClass.sortStudent(student1);
					for (Student s : student1) {
						textArea1.appendText(s.toString());
					}
				}

			}
		});

		button2.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent event) {
				String id = textField3.getText();
				String name = textField4.getText();
				textArea2.clear();
				if (checkTextF(id, name, textArea2)) {
					Student student = new Student(Integer.parseInt(id), name);
					for (int i = 0; i < student2.size(); i++) {
						if (student2.get(i).getID() == student.getID()) {
							textArea2.appendText("该学号的学生已存在！");
							return;
						} 
					}
					student2.add(student);
					adapterOfObject.sortStudent(student2);
					for (Student s : student2) {
						textArea2.appendText(s.toString());
					}
				}

			}
		});

		AnchorPane anchorPane = new AnchorPane();
		anchorPane.getChildren().addAll(label1, textField1, textField2, button1, textArea1, label2, textField3,
				textField4, button2, textArea2);
		primaryStage.setScene(new Scene(anchorPane, 560, 620));
		primaryStage.show();
	}

	public boolean checkTextF(String id, String name, TextArea textArea) {
		if (id.isEmpty() && name.isEmpty()) {
			textArea.appendText("请先输入学生学号和姓名！");
			return false;
		} else if ("".equals(id)) {
			textArea.appendText("请先输入学生的学号！");
			return false;
		} else if ("".equals(name)) {
			textArea.appendText("请先输入学生的姓名！");
			return false;
		} else if (!idIsInt(id)) {
			textArea.appendText("请输入正确的学号！");
			return false;
		}
		return true;
	}

	public boolean idIsInt(String id) {
		try {
			int temp = Integer.parseInt(id);
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	public static void main(String[] args) {
		launch(args);
	}
}
